<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="checkbox" :checked="checkedall" @change="setcheckedall" /> 全选
        <h3>
            请选择你的兴趣爱好
            <div v-for="item in list"><input type="checkbox" :value="item.id" v-model="item.checked" />{{item.hobby}}
            </div>

        </h3>
    </div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            checkedall: false,

            list: [{ id: 1, hobby: "篮球", checked: false }, { id: 2, hobby: "打游戏", checked: false }, { id: 3, hobby: "看小说", checked: false }, { id: 4, hobby: "睡觉", checked: false }],
        },

        methods: {
            setcheckedall(e) {
                console.log(e.target.checked)
                this.list.forEach(item => {
                    item.checked = e.target.checked;
                })
            }

        },

        watch: {

            // checkedall(flag) {
            //     this.list.forEach(item => {
            //         item.checked = flag;
            //     })
            // },

            list: {
                deep: true,
                handler(newList) {
                    // console.log(newList);
                    this.checkedall = newList.every(item => item.checked);
                }
            },

        }
    })

</script>

</html>